<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */
class SomeOrderModel extends Crud
{
    private $_listproduct;
    
    public function __construct()
    {
        parent::__construct();
    }

    /*===============================Данные по заказу================================*/  
    public function getOrderData($id)
    {
        $r = $this->get_one('tb_order', 'id', $id);
        foreach($r as $key =>$value)
        {
            $person =  $this->get_one('tb_person', 'id', $value['id_person']);
            if(empty($person))
            {
                $r[$key]['person'] = 'Пользователь удалён';
            }
            else
            {
                $r[$key]['person'] = $person[0]['name']." ".$person[0]['first_name']." ".$person[0]['second_name'];
            }
            $product = $this->get_one('tb_product', 'id', $value['id_product']);
            $r[$key]['product']  = $product[0]['name'];
            $this->_listproduct =$this->db->get_where('tb_product',array('id_category'=>$product[0]['id_category'], 'id_fabricator'=>$product[0]['id_fabricator']) ); 
            $fabricator = $this->get_one('tb_fabricator', 'id',$product[0]['id_fabricator']);
            $r[$key]['id_fabricator'] = $fabricator[0]['id'];
            $r[$key]['fabricator'] = $fabricator[0]['name'];
            $category = $this->get_one('gl_categories', 'id',$product[0]['id_category']);
            $r[$key]['id_category'] = $category[0]['id'];
            $r[$key]['category'] = $category[0]['name'];
            $r[$key]['cost'] =$product[0]['price'];
            $site = $this->get_one('gl_site', 'id',$r[$key]['id_site']);
            $r[$key]['site'] = $site[0]['name'];
        }
        return $r;
    }
    
    /*===============================Все категории=================================*/ 
    public function getCategory()
    {
        return $this->get_array('gl_categories');
    }
  
    /*===============================Все производители=============================*/
    public function getFabricator()
    {
       return $this->get_array('tb_fabricator');
    }
    
    /*===============Продукты соответс производителю и категории===================*/
    public function getProduct()
    {
        return $this->_listproduct->result_array();
    }
    
    /*=================================Все сайты===================================*/
    public function getSite()
    {
        return $this->get_array('gl_site');
    }
    
    /*===============================Список заказов================================*/   
    public function getReport($id)
    {
       $r['count_order'] = count($this->getOrderData($id));
       $q =  $this->getOrderData($id);   
       
       $r['total_cost'] = 0;
       foreach($q as $key => $value)
       {
            $temp = $this->get_one('tb_product', 'id', $value['id_product']);
            $r['total_cost']+= (float)$temp[0]['price'];
       }
       return $r;
    }
    
    /*===============================Список заказов================================*/
    public function orderToExcel($id)
    {
        $order_data = $this->getOrderData($id);
        //var_dump($order_data);
        
        $objPHPExcel = $this->ci_phpecxel;
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        
        $styleArray = array(
            'font' => array(
                'bold' => true,
                'size' => '16'
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
            ),
        );

        $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->applyFromArray($styleArray);
        
        $objPHPExcel->getActiveSheet()->setCellValue('A1','Код заказа');
        $objPHPExcel->getActiveSheet()->setCellValue('A2', $order_data[0]['id']); 
        
        $objPHPExcel->getActiveSheet()->setCellValue('B1','ФИО');
        $objPHPExcel->getActiveSheet()->setCellValue('B2', $order_data[0]['name']); 
        
        $objPHPExcel->getActiveSheet()->setCellValue('C1','Дата заказа');
        $objPHPExcel->getActiveSheet()->setCellValue('C2', $order_data[0]['date_create']); 
        
        $objPHPExcel->getActiveSheet()->setCellValue('D1','Название продукта');
        $objPHPExcel->getActiveSheet()->setCellValue('D2', $order_data[0]['product']);  
        
        $objPHPExcel->getActiveSheet()->setCellValue('E1','Производитель');
        $objPHPExcel->getActiveSheet()->setCellValue('E2', $order_data[0]['fabricator']);  
        
        $objPHPExcel->getActiveSheet()->setCellValue('F1','Цена');
        $objPHPExcel->getActiveSheet()->setCellValue('F2', $order_data[0]['cost']);   
        
        $objPHPExcel->getActiveSheet()->setTitle('Заказ');
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $path ='doc/order/';
        $filename = 'orderdata-'.$id.'.xlsx';
        $objWriter->save($path.$filename);
        
        return "<a href='/$path$filename'>$filename</a>";
    }
}